<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>包头希望铝业电解铝车间管理</span>
        <el-button 
          style="float: right; padding: 3px 0" 
          type="text" 
          icon="el-icon-refresh"
          @click="refreshData(true)"
          :loading="loading"
        >刷新</el-button>
      </div>
      
      <!-- 工段信息网格展示 -->
      <div class="sections-grid">
        <div 
          v-for="section in sectionsList" 
          :key="section.sectionName"
          class="section-card"
          @click="viewSectionSlots(section)"
        >
          <!-- 工段头部 -->
          <div class="section-header">
            <div class="section-title">
              <i class="el-icon-s-grid"></i>
              {{ section.sectionName }}
            </div>
          </div>
          
          <!-- 工段基本信息 -->
          <div class="section-stats">
            <div class="info-row">
              <span class="info-label">范围：</span>
              <span class="info-value">{{ section.startSlotNo }}-{{ section.endSlotNo }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">总槽数：</span>
              <span class="info-value">{{ section.slotCount }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">每槽点数：</span>
              <span class="info-value">{{ section.pointsPerSlot }}</span>
            </div>
          </div>
          
          <!-- 状态统计 -->
          <div class="section-stats">
            <div class="stat-item error">
              <span class="stat-label">异常槽数</span>
              <span class="stat-value">{{ section.errorSlotCount }}</span>
            </div>
            <div class="stat-item success">
              <span class="stat-label">下料点位</span>
              <span class="stat-value">{{ section.dischargePointCount }}</span>
            </div>
            <div class="stat-item warning">
              <span class="stat-label">卡堵点位</span>
              <span class="stat-value">{{ section.jamPointCount }}</span>
            </div>
          </div>
          
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getSectionSlots } from "@/api/workshop/section";

export default {
  name: "Workshop",
  data() {
    return {
      // 遮罩层
      loading: false,
      refreshLoading: false,
      // 定时器
      refreshTimer: null,
      // 工段列表数据
      sectionsList: [
        {
          sectionName: '11段',
          startSlotNo: 1001,
          endSlotNo: 1075,
          slotCount: 75,
          pointsPerSlot: 4,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '12段',
          startSlotNo: 1076,
          endSlotNo: 1151,
          slotCount: 76,
          pointsPerSlot: 4,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '21段',
          startSlotNo: 2001,
          endSlotNo: 2075,
          slotCount: 75,
          pointsPerSlot: 4,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '22段',
          startSlotNo: 2076,
          endSlotNo: 2151,
          slotCount: 76,
          pointsPerSlot: 4,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '31段',
          startSlotNo: 3001,
          endSlotNo: 3075,
          slotCount: 75,
          pointsPerSlot: 4,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '32段',
          startSlotNo: 3076,
          endSlotNo: 3151,
          slotCount: 76,
          pointsPerSlot: 4,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '41段',
          startSlotNo: 4001,
          endSlotNo: 4075,
          slotCount: 75,
          pointsPerSlot: 4,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '42段',
          startSlotNo: 4076,
          endSlotNo: 4151,
          slotCount: 76,
          pointsPerSlot: 4,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '51段',
          startSlotNo: 5001,
          endSlotNo: 5077,
          slotCount: 77,
          pointsPerSlot: 6,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '52段',
          startSlotNo: 5078,
          endSlotNo: 5154,
          slotCount: 77,
          pointsPerSlot: 6,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '61段',
          startSlotNo: 6001,
          endSlotNo: 6077,
          slotCount: 77,
          pointsPerSlot: 6,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        },
        {
          sectionName: '62段',
          startSlotNo: 6078,
          endSlotNo: 6154,
          slotCount: 77,
          pointsPerSlot: 6,
          errorSlotCount: 0,
          dischargePointCount: 0,
          jamPointCount: 0
        }
      ],
      // 槽位对话框
      currentSection: null,
      slotList: []
    };
  },
  created() {
    // 页面初始化时加载数据
    this.refreshData();
    // 启动定时刷新
    this.startAutoRefresh();
  },
  beforeDestroy() {
    // 清理定时器
    this.stopAutoRefresh();
  },
  methods: {
    
    /** 查看工段槽位 */
    viewSectionSlots(section) {
      // 跳转到工段详情页面
      this.$router.push({
        path: '/workshop/section',
        query: { sectionName: section.sectionName }
      });
    },
    
    
    /** 刷新数据 */
    async refreshData(showMessage = false) {
      this.loading = true;
      try {
        // 并行查询所有工段的数据
        const sectionNames = this.sectionsList.map(section => section.sectionName);
        const promises = sectionNames.map(sectionName => 
          getSectionSlots(sectionName).catch(error => {
            console.error(`获取工段 ${sectionName} 数据失败:`, error);
            return { data: [] }; // 返回空数据，避免整个刷新失败
          })
        );
        
        const results = await Promise.all(promises);
        
        // 更新每个工段的统计数据
        this.sectionsList.forEach((section, index) => {
          const slotData = results[index].data || [];
          this.updateSectionStats(section, slotData);
        });
        
        // 只有在手动刷新时才显示成功消息
        if (showMessage) {
          this.$message.success('数据刷新成功');
        }
      } catch (error) {
        console.error('刷新数据失败:', error);
        if (showMessage) {
          this.$message.error('刷新数据失败: ' + error.message);
        }
      } finally {
        this.loading = false;
      }
    },
    
    /** 更新工段统计数据 */
    updateSectionStats(section, slotData) {
      if (!slotData || slotData.length === 0) {
        // 如果没有数据，重置为0
        section.errorSlotCount = 0;
        section.dischargePointCount = 0;
        section.jamPointCount = 0;
        return;
      }
      
      // 计算异常槽数（电源箱门或槽控机门打开）
      section.errorSlotCount = slotData.filter(slot => 
        slot.powerBoxDoorOpen || slot.controllerDoorOpen
      ).length;
      
      // 计算下料点位数量
      section.dischargePointCount = 0;
      slotData.forEach(slot => {
        if (slot.pointInfos) {
          section.dischargePointCount += slot.pointInfos.filter(point => 
            point.dischargeStatus
          ).length;
        }
      });
      
      // 计算卡堵点位数量
      section.jamPointCount = 0;
      slotData.forEach(slot => {
        if (slot.pointInfos) {
          section.jamPointCount += slot.pointInfos.filter(point => 
            point.jamStatus
          ).length;
        }
      });
    },
    
    /** 启动自动刷新 */
    startAutoRefresh() {
      // 清除现有定时器
      this.stopAutoRefresh();
      // 设置1秒定时刷新
      this.refreshTimer = setInterval(() => {
        this.refreshData(false); // 定时刷新不显示消息
      }, 3000);
    },
    
    /** 停止自动刷新 */
    stopAutoRefresh() {
      if (this.refreshTimer) {
        clearInterval(this.refreshTimer);
        this.refreshTimer = null;
      }
    },
    
  }
};
</script>

<style scoped>

.slot-container {
  padding: 20px;
}

.slot-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}

.section-info h3 {
  margin: 0 0 10px 0;
  color: #303133;
}

.section-details {
  display: flex;
  gap: 20px;
  color: #606266;
  font-size: 14px;
}

.slot-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 15px;
  max-height: 600px;
  overflow-y: auto;
}

.slot-item {
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  padding: 15px;
  background: #fff;
  transition: all 0.3s;
}

.slot-item:hover {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.slot-item.slot-error {
  border-color: #f56c6c;
  background: #fef0f0;
}

.slot-header-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.slot-number {
  font-weight: bold;
  color: #303133;
}

.door-status {
  display: flex;
  gap: 5px;
}

.points-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.point-item {
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  padding: 8px;
  text-align: center;
  background: #f5f7fa;
  transition: all 0.3s;
}

.point-item.point-discharge {
  background: #e1f3d8;
  border-color: #67c23a;
}

.point-item.point-jam {
  background: #fde2e2;
  border-color: #f56c6c;
}

.point-number {
  font-weight: bold;
  color: #303133;
  margin-bottom: 4px;
}

.point-status {
  font-size: 12px;
}

.status-discharge {
  color: #67c23a;
  font-weight: bold;
}

.status-jam {
  color: #f56c6c;
  font-weight: bold;
}

/* 新的工段卡片样式 */
.sections-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.section-card {
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  padding: 12px;
  background: #fff;
  transition: all 0.3s;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
}

.section-card:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
  border-color: #409eff;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f2f5;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
  display: flex;
  align-items: center;
  gap: 6px;
}

.section-status {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
  background: #f0f9ff;
  color: #409eff;
}

.section-status.status-error {
  background: #fef0f0;
  color: #f56c6c;
}

.section-info {
  margin-bottom: 16px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
  padding: 2px 0;
}

.info-label {
  color: #606266;
  font-size: 12px;
  font-weight: 500;
}

.info-value {
  color: #303133;
  font-size: 12px;
  font-weight: bold;
}

.section-stats {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: nowrap;
}

.stat-item {
  flex: 1;
  min-width: 60px;
  text-align: center;
  padding: 6px;
  border-radius: 4px;
  background: #f8f9fa;
}

.stat-item.error {
  background: #fef0f0;
  border: 1px solid #fde2e2;
}

.stat-item.success {
  background: #f0f9ff;
  border: 1px solid #b3d8ff;
}

.stat-item.warning {
  background: #fdf6ec;
  border: 1px solid #f5dab1;
}

.stat-label {
  display: block;
  font-size: 10px;
  color: #909399;
  margin-bottom: 2px;
}

.stat-value {
  display: block;
  font-size: 14px;
  font-weight: bold;
  color: #303133;
}

.stat-item.error .stat-value {
  color: #f56c6c;
}

.stat-item.success .stat-value {
  color: #67c23a;
}

.stat-item.warning .stat-value {
  color: #e6a23c;
}


/* 响应式设计 */
@media (max-width: 1200px) {
  .sections-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 900px) {
  .sections-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 600px) {
  .sections-grid {
    grid-template-columns: 1fr;
  }
  
  .section-stats {
    flex-direction: column;
  }
  
  .stat-item {
    min-width: auto;
  }
}
</style>
